package com.dragon.user_service.util;

import com.dragon.user_service.Exception.BusinessException;
import com.dragon.user_service.Exception.StateCode;
import com.dragon.user_service.api.RPC.permissionService;
import com.dragon.user_service.domain.Enum.UserRoleEnum;
import com.dragon.user_service.domain.entity.User;
import com.dragon.user_service.domain.vo.user.UserVO;
import com.dragon.user_service.service.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Component
public class AuthCheckUtil{
    @Resource
    permissionService permissionService;
    @Resource
    UserService userService;
    public void checkAuth(HttpServletRequest request,Long t_uid){

        User loginUser = userService.getLoginUser(request);
        Long login_uid = loginUser.getUser_id();

        //RPC调用查询我的角色
        String loginUserRoleCode = permissionService.getUserRoleCode(login_uid);
        //RPC调用查询要操作的用户的角色
        String targetUserRoleCode = permissionService.getUserRoleCode(t_uid);
        //如果我的角色比它大,那我就可以操作它,那么怎么比较角色大小呢?这是两个枚举类型
        UserRoleEnum loginUserRoleEnum = UserRoleEnum.getEnumByValue(loginUserRoleCode);
        UserRoleEnum targetUserRoleEnum = UserRoleEnum.getEnumByValue(targetUserRoleCode);
        if(loginUserRoleEnum==null||targetUserRoleEnum==null){
            throw new BusinessException(StateCode.NO_AUTH_ERROR);
        }
        //如果是超管,可以操作所有用户
        if(loginUserRoleEnum.equals(UserRoleEnum.SUPER_ADMIN)){
            return ;
        }
        //如果是管理员用户,但是操作超管,无权限
        if(loginUserRoleEnum.equals(UserRoleEnum.ADMIN)&&targetUserRoleEnum.equals(UserRoleEnum.SUPER_ADMIN)){
            throw new BusinessException(StateCode.NO_AUTH_ERROR,"无权限操作该用户");
        }
        //如果是普通用户,但是操作非普通用户,无权限
        if(loginUserRoleEnum.equals(UserRoleEnum.User)&&!targetUserRoleEnum.equals(UserRoleEnum.User)){
            throw new BusinessException(StateCode.NO_AUTH_ERROR,"无权限操作该用户");
        }
        //如果是普通用户,操作的也是普通用户,那只能操作自己,否则报错
        if(loginUserRoleEnum.equals(UserRoleEnum.User)&& !login_uid.equals(t_uid)){
            throw new BusinessException(StateCode.NO_AUTH_ERROR,"无权限操作该用户");
        }
    }
    public UserVO filter(String loginUserRoleCode,UserVO userVO){
        Long t_uid=userVO.getUser_id();
        //RPC调用查询要操作的用户的角色
        String targetUserRoleCode = permissionService.getUserRoleCode(t_uid);
        //如果我的角色比它大,那我就可以操作它,那么怎么比较角色大小呢?这是两个枚举类型
        UserRoleEnum loginUserRoleEnum = UserRoleEnum.getEnumByValue(loginUserRoleCode);
        UserRoleEnum targetUserRoleEnum = UserRoleEnum.getEnumByValue(targetUserRoleCode);
        if(loginUserRoleEnum==null||targetUserRoleEnum==null){
            throw new BusinessException(StateCode.NO_AUTH_ERROR);
        }
        //如果是普通用户,但是操作非普通用户,无权限
        if(loginUserRoleEnum.equals(UserRoleEnum.User)&&!targetUserRoleEnum.equals(UserRoleEnum.User)){
            return null;
        }
        //如果是管理员用户,但是操作超管,无权限
        if(loginUserRoleEnum.equals(UserRoleEnum.ADMIN)&&targetUserRoleEnum.equals(UserRoleEnum.SUPER_ADMIN)){
            return null;
        }
        return userVO;
    }
    //分页查询过滤处理,对所有查出来的user进行一个过滤,根据角色的权限进行过滤
    public List<UserVO> filterList(HttpServletRequest request, List<UserVO> userVOs){
        User loginUser = userService.getLoginUser(request);
        Long login_uid = loginUser.getUser_id();
        //RPC调用,获得用户的权限码
        String loginUserRoleCode = permissionService.getUserRoleCode(login_uid);

        List<UserVO> userVoList=new ArrayList<>();
        //进行过滤
        for (UserVO userVo : userVOs) {
            UserVO check = filter(loginUserRoleCode, userVo);
            if(check!=null){
                userVoList.add(check);
            }
        }
        return userVoList;
    }
}
